package com.chuanjiabao.wisdom.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.ColorBean;
import com.chuanjiabao.wisdom.databinding.ItemColorBinding;
import com.chuanjiabao.wisdom.listener.OnItemClickListener;

import java.util.List;

public class ColorAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<ColorBean> strings;
    private Context mcontext;
    OnItemClickListener mOnItemClickLitener;

    public ColorAdapter(Context context, List<ColorBean> strings) {
        this.mcontext = context;
        this.strings = strings;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemColorBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mcontext),
                R.layout.item_color, parent, false);
        binding.setAdapter(this);
        return new TourViewHolder(binding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData(strings.get(position));
        final TourViewHolder tv = (TourViewHolder) holder;

        switch (position){
            case 0:
                tv.vModuleSelect.setBackgroundResource(R.drawable.module_draw_color_first);
                break;
            case 1:
                tv.vModuleSelect.setBackgroundResource(R.drawable.module_draw_color_second);
                break;
            case 2:
                tv.vModuleSelect.setBackgroundResource(R.drawable.module_draw_color_third);
                break;
            case 3:
                tv.vModuleSelect.setBackgroundResource(R.drawable.module_draw_color_forth);
                break;
            case 4:
                tv.vModuleSelect.setBackgroundResource(R.drawable.module_draw_color_fifth);
                break;
            case 5:
                tv.vModuleSelect.setBackgroundResource(R.drawable.module_draw_color_sixth);
                break;
            case 6:
                tv.vModuleSelect.setBackgroundResource(R.drawable.module_draw_color_seventh);
                break;
            case 7:
                tv.vModuleSelect.setBackgroundResource(R.drawable.module_draw_color_eighth);
                break;
        }

        boolean isCheck = strings.get(position).isCheck();
        if (isCheck) tv.vModuleNoSelect.setBackgroundResource(R.drawable.module_draw_color_select);
        else tv.vModuleNoSelect.setBackgroundResource(R.drawable.module_draw_color_no_select);

        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(v -> {
                int pos = holder.getLayoutPosition();

                for (ColorBean item : strings) { item.setCheck(false); }
                strings.get(pos).setCheck(true);
                notifyDataSetChanged();

                mOnItemClickLitener.onItemClick(holder.itemView, pos);
            });
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<ColorBean> {
        ItemColorBinding functionBinding;
        View vModuleSelect, vModuleNoSelect;

        public TourViewHolder(ItemColorBinding dataBinding) {
            super(dataBinding.getRoot());
            functionBinding = dataBinding;
            vModuleSelect = dataBinding.vModuleSelect;
            vModuleNoSelect = dataBinding.vModuleNoSelect;
        }

        @Override
        public void bindData(ColorBean data) {
            functionBinding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }
}